package com.opal.modules.costReportForm.controller;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.domain.ReturnMessageData;
import com.opal.model.DataGridData;
import com.opal.modules.costReportForm.entity.CostReport;
import com.opal.modules.costReportForm.entity.CostReportCondition;
import com.opal.modules.costReportForm.exception.CostReportUploadException;
import com.opal.modules.costReportForm.exception.DeleteCostReportException;
import com.opal.modules.costReportForm.model.CostReportData;
import com.opal.modules.costReportForm.service.CostReportService;
import com.opal.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("costReport")
@Slf4j
public class CostReportController {
    @Autowired
    private CostReportService costReportService;

    /**
     * 新增成本报告表：保存
     * @param costReport
     * @param costReportData
     * @return
     */
    @RequestMapping("/costReportSave")
    @ResponseBody
    public Object costReportSave(CostReport costReport,CostReportData costReportData){

        return costReportService.costReportSave(costReport,costReportData);

    }

    /**
     * 成本报告表：保存并审核
     * @param costReport
     * @param costReportData
     * @return
     * @throws IOException
     */
    @RequestMapping("/costReportSaveAudit")
    @ResponseBody
    public Object costReportSaveAudit(CostReport costReport, CostReportData costReportData) throws Exception {

        return costReportService.costReportSaveAuditService(costReport,costReportData);
    }

    /**
     * 成本报告表：停用/启用
     * @param costReportId
     * @return
     * @throws IOException
     */
    @RequestMapping("/SUCostReport")
    @ResponseBody
    public Object SUCostReport(String costReportId) {
        R cr = costReportService.SUCostReport(costReportId);
            return cr;
    }

    /**
     * 成本报告表：修改
     * @param costReport
     * @return
     * @throws IOException
     */
    @RequestMapping("/updateCostReport")
    @ResponseBody
    public Object updateCostReport(CostReport costReport) {
        CostReport cr = costReportService.isCostReportStatusService(costReport.getCostReportId());
        if (cr.getCostReportStatus()==1){
            return new R(WSC.PRECONDITIONFAILED,"成本报告已封存，不允许修改");
        }
        return costReportService.updateCostReportService(costReport);
    }

    /**
     * 添加/更换 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("costReportUploadFile")
    @ResponseBody
    public  R costReportUploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        return  costReportService.costReportUploadFile(file);
    }

    /**
     * 单纯删除上传的文件
     * @param filePath
     * @return
     */
    @RequestMapping("deleteCostReportUploadFile")
    @ResponseBody
    public  R deleteCostReportUploadFile(String filePath){
        return costReportService.deleteCostReportUploadFile(filePath);
    }



    /**
     * 根据条件查询成本报告表
     * @param costReport    条件
     * @param dataGrid  分页rows和page
     * @return  easyui的表格
     */
    @RequestMapping("pagingQueryCostReport")
    public Object pagingQueryCostReport(CostReportCondition costReport, DataGridData<Integer> dataGrid){
        return costReportService.pagingQueryCostReportService(costReport,dataGrid);
    }

    /**
     * 根据条件查询成本报告表 ---用于刷选报价单的数据
     * @param costReport    条件
     * @param page  页面
     * @param limit  每月数据
     * @return  easyui的表格
     */
    @RequestMapping("queryCostReport")
    public Object queryCostReport(CostReportCondition costReport, Integer page,Integer limit){
        return costReportService.pagingQueryCostReportService(costReport,page,limit);
    }

    /**
     * 删除所有关于此成本单据号的信息
     * @param costReportId  成本单据号
     * @return  R ：msg消息，status 状态
     */
    @RequestMapping("deleteCostReport")
    public Object deleteCostReport(String costReportId){
        return costReportService.deleteCostReportService(costReportId);
    }

    /**
     * 进行关闭easyui-datagrid，进行判断有没有填写必填项，若没有证明是用户放弃本次的成本报告输入
     * @return  R   msg：消息，status 状态
     */
    @RequestMapping("deleteFieldIsNullByCostReportId")
    public Object deleteFieldIsNullByCostReportId( String costReportId,String costImagePath){
        return costReportService.deleteFieldIsNullByCostReportIdService(costReportId,costImagePath);
    }

    /**
     * 修改成本报告表：判断状态
     * @param banben
     * @return
     * @throws IllegalAccessException
     */
    @RequestMapping("/isCostReportStatus")
    @ResponseBody
    public Object isCostReportStatus(String banben){
        return costReportService.isCostReportStatusService(banben);
    }

    /**
     * 控制按钮生成：审批详情-任务-操作的按钮：拒绝状态3：可用于修改成本报告表，同意状态1：审批按钮，
     * @param processInstanceId
     * @return
     */
    @RequestMapping("/getApproverOperationStatus")
    @ResponseBody
    public Object getApproverOperationStatus(String processInstanceId){
        return  costReportService.getApproverOperationStatusService(processInstanceId);
    }

    /**
     * 根据有权限的人或本人修改报价单编号
     * @param costReport
     * @return
     */
    @RequestMapping("/updateQuotation")
    @ResponseBody
    public Object updateQuotation(CostReport costReport){
       return  costReportService.updateQuotationService(costReport);
    }

    /**
     * 根据成本报告单据号和用户id，修改授权人
     * @param userId
     * @param costReportId
     * @return
     */
    @RequestMapping("/updateAppointUserId")
    @ResponseBody
    public Object updateAppointUserId(Long userId,String costReportId){
       return  costReportService.updateAppointUserIdService(userId,costReportId);
    }

    /**
     * 管理员直接进行给成本报告进行授权修改成报价单编号
     * @param userId
     * @param costReportId
     * @return
     */
    @RequestMapping("/updateSystemAppointUserId")
    @ResponseBody
    public Object updateSystemAppointUserId(Long userId,String costReportId){
        return  costReportService.updateSystemAppointUserIdService(userId,costReportId);
    }

    /**
     * 授权页面获取layui表格数据
     * @param costReport 成本报告对象
     */
    @RequestMapping("/selectAccreditUpdateQuotation")
    @ResponseBody
    public Object selectAccreditUpdateQuotation(CostReport costReport,Integer page,Integer limit,String userName){
        return  costReportService.selectAccreditUpdateQuotationService(costReport,(page-1)*limit,limit,userName);
    }

    /**
     * 保存与保存并提交出现的异常情况
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public Object costException(Exception e){
        e.printStackTrace();
        if (e instanceof IOException){
            log.error("用户：【" + UserUtils.getSessionUserId() + "】启动流程失败了");
            return new ReturnMessageData(WSC.REQUEST_ERROR, "审核提交失败：数据不完整！");

        }
        if (e instanceof ActivitiException){

            log.error("用户：【" + UserUtils.getSessionUserId() + "】启动流程失败了");
            return new ReturnMessageData(WSC.REQUEST_ERROR, "提交申请失败：申请人或审批人未设置邮箱！");
        }

        if (e instanceof CostReportUploadException){
            return  new R(WSC.REQUEST_ERROR,e.getMessage());
        }

        return new ReturnMessageData(WSC.REQUEST_ERROR, e.getMessage());
    }


    /**
     * 删除操作异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = {DeleteCostReportException.class})
    @ResponseBody
    public Object DeleteCostReportException(DeleteCostReportException e){
            return new R(WSC.REQUEST_ERROR,e.getMessage());
    }

}
